package cn.lystudio.rohmtimeopwide_background_v11.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.lystudio.rohmtimeopwide_background_v11.entity.dto.SideBarMenuDTO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.po.RolesSideBarPO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.po.SideBarMenuPO;
import cn.lystudio.rohmtimeopwide_background_v11.mapper.RolesSideBarPOMapper;
import cn.lystudio.rohmtimeopwide_background_v11.mapper.SideBarMenuPOMapper;
import cn.lystudio.rohmtimeopwide_background_v11.service.SideBarService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
@DS("master")

public class SideBarServiceImpl implements SideBarService {
    @Resource
    private RolesSideBarPOMapper rolesSideBarPOMapper;
    @Resource
    private SideBarMenuPOMapper sideBarMenuPOMapper;

    @Override
    public List<SideBarMenuDTO> getSideBar(Integer roleID) {
        List<SideBarMenuDTO> sideBarMenuDTOList = ListUtil.list(false);
        // 获取用户的部门ID List
        List<String> topDeptIDList = StpUtil.getPermissionList(StpUtil.getLoginIdAsString());

        // 获取用户角色的子菜单
        List<RolesSideBarPO> rolesSideBarList = rolesSideBarPOMapper.selectList(new QueryWrapper<RolesSideBarPO>().lambda().eq(RolesSideBarPO::getRoleID, roleID).orderByAsc(RolesSideBarPO::getRoleID).orderByAsc(RolesSideBarPO::getSideBarID));
        // 将子菜单ID组合成一个ID List
        List<Integer> childrenMenuIDList = CollUtil.list(false);
        CollUtil.forEach(rolesSideBarList.iterator(), (rolesSideBarPO, index) -> {
            childrenMenuIDList.add(rolesSideBarPO.getSideBarID());
        });
        if (topDeptIDList.contains("144")) { // SSD1组立
            CollUtil.filter(childrenMenuIDList, (childrenMenuID) -> {
                if (childrenMenuID.toString().equals("8252")) return false;
                if (childrenMenuID.toString().equals("8253")) return false;
                return true;
            });
        }

        if (topDeptIDList.contains("146")) {     //SSD1半田
            CollUtil.filter(childrenMenuIDList, (childrenMenuID) -> {
                if (childrenMenuID.toString().equals("8251")) return false;
                if (childrenMenuID.toString().equals("8253")) return false;
                return true;
            });
        }
        if (topDeptIDList.contains("147")) {     //SSD1测定
            CollUtil.filter(childrenMenuIDList, (childrenMenuID) -> {
                if (childrenMenuID.toString().equals("8251")) return false;
                if (childrenMenuID.toString().equals("8252")) return false;
                return true;
            });
        }
        if((!topDeptIDList.contains("144")) && (!topDeptIDList.contains("146")) && (!topDeptIDList.contains("147")) && (!topDeptIDList.contains("1"))) {
            CollUtil.filter(childrenMenuIDList, (childrenMenuID) -> {
                if (childrenMenuID.toString().equals("8251")) return false;
                if (childrenMenuID.toString().equals("8252")) return false;
                if (childrenMenuID.toString().equals("8253")) return false;
                return true;
            });
        }
        // 获取所有顶级菜单
        List<SideBarMenuPO> topMenuList = sideBarMenuPOMapper.selectList(new QueryWrapper<SideBarMenuPO>().lambda().eq(SideBarMenuPO::getParentID, 0).orderByAsc(SideBarMenuPO::getId));

        // 遍历顶级菜单,添加子菜单
        CollUtil.forEach(topMenuList.iterator(), (topMenu, index) -> {
            SideBarMenuDTO sideBarMenuDTO = new SideBarMenuDTO();
            sideBarMenuDTO.setId(topMenu.getId());
            sideBarMenuDTO.setTitle(topMenu.getTitle());
            sideBarMenuDTO.setIcon(topMenu.getIcon());
            sideBarMenuDTO.setType(topMenu.getType());
            sideBarMenuDTO.setHref(topMenu.getHref());
            LambdaQueryWrapper<SideBarMenuPO> wrapper = new QueryWrapper<SideBarMenuPO>().lambda();
            wrapper.eq(SideBarMenuPO::getParentID, topMenu.getId())
                    .in(SideBarMenuPO::getId, childrenMenuIDList)
                    .orderByAsc(SideBarMenuPO::getId);
            List<SideBarMenuPO> sonList = sideBarMenuPOMapper.selectList(wrapper);
            sideBarMenuDTO.setChildren(sonList);
            sideBarMenuDTOList.add(sideBarMenuDTO);
        });

        //  过滤掉没有子菜单的母菜单
        CollUtil.filter(sideBarMenuDTOList, sideBarMenuDTO -> {
            if (ObjectUtil.isEmpty(sideBarMenuDTO.getChildren())) return false;
            return true;
        });

        return sideBarMenuDTOList;

    }
}
